package com.example.lenovo.wechat;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageButton mWeChat;
    private ImageButton mContract;
    private ImageButton mFind;
    private ImageButton mMe;

    private List<Msg> msgList=new ArrayList<>();
    private EditText inputText;
    private Button send;

    private RecyclerView msgRecyclerView;
    private MsgAdapter adapter;



    //private WeChatFragment weChatFragment=null;
    private ContractFragment contractFragment=null;
    private FindFragment findFragment=null;
    private MeFragment meFragment=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_new);
        mWeChat=findViewById(R.id.btn_wechat);
        mContract=findViewById(R.id.btn_contact);
        mFind=findViewById(R.id.btn_find);
        mMe=findViewById(R.id.btn_me);

        mWeChat.setOnClickListener(this);
        mContract.setOnClickListener(this);
        mFind.setOnClickListener(this);
        mMe.setOnClickListener(this);

        selectTab(0);

        initMsgs();
        inputText=(EditText) findViewById(R.id.input_text);
        send=(Button) findViewById(R.id.send);
        msgRecyclerView=(RecyclerView) findViewById(R.id.msg_recycler_view);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        msgRecyclerView.setLayoutManager(layoutManager);
        adapter=new MsgAdapter(msgList);
        msgRecyclerView.setAdapter(adapter);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content=inputText.getText().toString();
                if(!"".equals(content)){
                    Msg msg=new Msg(content,Msg.TYPE_SENT);
                    msgList.add(msg);
                    adapter.notifyItemInserted(msgList.size()-1);
                    msgRecyclerView.scrollToPosition(msgList.size()-1);
                    inputText.setText("");
                }
            }
        });
    }

    private void initMsgs() {
        Msg msg1=new Msg("Hello guy.",Msg.TYPE_RECEIVED);
        msgList.add(msg1);
        Msg msg2=new Msg("Hello. who is that?",Msg.TYPE_SENT);
        msgList.add(msg2);
        Msg msg3=new Msg("This is Tom. Nice talking to you.",Msg.TYPE_RECEIVED);
        msgList.add(msg3);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.btn_wechat:
                selectTab(0);
               break;
            case R.id.btn_contact:
                selectTab(1);
                break;
            case R.id.btn_find:
                selectTab(2);
                break;
            case R.id.btn_me:
                selectTab(3);
                break;
                default:
                    Log.d("MainActivity","on click error!");
        }
    }

    private void selectTab(int index) {
        setTabButtonDefaultColor();
        FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
        hideFragments(transaction);
        switch(index){
            case 0:
                mWeChat.setImageResource(R.mipmap.tab_weixin_pressed);
              /*  if(weChatFragment==null){
                    weChatFragment=new WeChatFragment();
                    transaction.add(R.id.fl_content,weChatFragment);
                }else {
                   transaction.show(weChatFragment);
                } */
                break;
            case 1:
                mContract.setImageResource(R.mipmap.tab_address_pressed);
                if(contractFragment==null){
                    contractFragment =new ContractFragment();
                    transaction.add(R.id.fl_content,contractFragment);
                }else {
                    transaction.show(contractFragment);
                }
                break;
            case 2:
                mFind.setImageResource(R.mipmap.tab_find_frd_pressed);
                if(findFragment==null){
                    findFragment =new FindFragment();
                    transaction.add(R.id.fl_content,findFragment);
                }else {
                    transaction.show(findFragment);
                }
                break;
            case 3:
                mMe.setImageResource(R.mipmap.tab_settings_pressed);
                if(meFragment==null){
                    meFragment =new MeFragment();
                    transaction.add(R.id.fl_content,meFragment);
                }else {
                    transaction.show(meFragment);
                }
                break;
        }
        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {
       if(weChatFragment!=null){
           transaction.hide(weChatFragment);
       }
       if(contractFragment!=null){
           transaction.hide(contractFragment);
       }
       if(findFragment!=null){
           transaction.hide(findFragment);
       }
       if(meFragment!=null){
           transaction.hide(meFragment);
       }
    }

    private void setTabButtonDefaultColor() {
        mWeChat.setImageResource(R.mipmap.tab_weixin_normal);
        mContract.setImageResource(R.mipmap.tab_address_normal);
        mFind.setImageResource(R.mipmap.tab_find_frd_normal);
        mMe.setImageResource(R.mipmap.tab_settings_normal);
    }
}
